/*
* SIP XML Scenario Maker

* Copyright 2008 Rachid El Khayari

*

* This file is part of SIP XML Scenario Maker (SXSM).

*

* SXSM is free software: you can redistribute it and/or modify

* it under the terms of the GNU General Public License as published by

* the Free Software Foundation, either version 3 of the License, or

* (at your option) any later version.

*

* SXSM is distributed in the hope that it will be useful,

* but WITHOUT ANY WARRANTY; without even the implied warranty of

* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

* GNU General Public License for more details.

*

* You should have received a copy of the GNU General Public License

* along with SXSM.  If not, see <http://www.gnu.org/licenses/>.

*/
#include <QtGui>
#include <QLineEdit>
#include <QGridLayout>
#include <QLabel>
#include <QPushButton>
#include <QComboBox>
#include <QGroupBox>
#include <QTextEdit>
#include <QProcess>
#include <QStringList>
#include <QListWidget>
#include "sxsm.h"


sxsm::sxsm()
{
	/*!
 	* Defines main grid layout for the central widget.
 	*/
	QGridLayout *grid = new QGridLayout; //define main grid layout
	/*!
 	* Loads settings parameters.
 	*/
	QSettings settings("RashSoft","sxsm");
	SIPpPath = settings.value("SIPp Path").toString();
	DataDir = settings.value("datadir").toString();
	ScenarioDir = settings.value("scenariodir").toString();
	MessageDir = settings.value("messagedir").toString();
	CSVDir = settings.value("csvdir").toString();
	LogDir = settings.value("logdir").toString();
	/*!
 	* Creates group boxes of the central widget.
 	*/
	grid->addWidget(createScenarioMakerGroupBox(), 0, 0);
	grid->addWidget(createScenarioViewerGroupBox(), 0, 1);
	/*!
 	* Sets the column stretch to 3:4 so that the ScenarioViewerGroupBox
	* gets enough space.
 	*/
	grid->setColumnStretch(0,3);
	grid->setColumnStretch(1,4);
	setLayout(grid);
}

QGroupBox *sxsm::createScenarioMakerGroupBox()
{
	/*!
	* Places buttons, combo boxes, etc. in scenario maker group box.
	*/
	scenarioDetailLayout=new QGridLayout();
	QGroupBox *scenarioGroupBox = new QGroupBox(tr("ScenarioMaker"));
	scenarioMessageSet = new QComboBox;
	scenarioMessageSet->addItem("Select message set");
	scenarioMessageType = new QComboBox;
	scenarioMessageType->addItem("Select message type");
	scenarioMessageType->setDisabled(true);
	scenarioMessage = new QComboBox;
	scenarioMessage->addItem("Select message");
	scenarioMessage->setDisabled(true);
	//scenarioNewNameLabel = new QLabel(tr("Name for new Scenario"));
	//scenarioNewName = new QLineEdit(tr("Enter name"));
	//scenarioNewName->setDisabled(true);
	addMessageButton = new QPushButton(tr("Add"));
	scenarioElementDeleteButton = new QPushButton(tr("Delete selected"));
	scenarioElementDeleteButton->setDisabled(true);
	scenarioListWidget=new QListWidget(this);
	scenarioListWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
	scenarioListWidget->setDisabled(true);
	scenarioDetailLayout->addWidget(scenarioMessageSet, 0, 0);
	scenarioDetailLayout->addWidget(scenarioMessageType, 0, 1);
	scenarioDetailLayout->addWidget(scenarioMessage, 0, 2);
	scenarioDetailLayout->addWidget(addMessageButton, 1, 2);
	scenarioDetailLayout->addWidget(scenarioElementDeleteButton, 1, 0);
	scenarioDetailLayout->addWidget(scenarioListWidget, 2, 0, 1, 3);
	//scenarioDetailLayout->addWidget(scenarioNewNameLabel, 3, 0);
	//scenarioDetailLayout->addWidget(scenarioNewName, 3, 1, 1, 2);
	if(!makeMessageSetCombo()){
		QMessageBox::StandardButton msgBox;
		msgBox = QMessageBox::warning(this, tr("Message set list not loaded!"),
                      tr("Couldn't find messageset.dat.\n"
                         "in message directory!"),
                      QMessageBox::Ok);
	}
	scenarioMakeButton=new QPushButton(tr("Make scenario"), this);
	scenarioMakeButton->setDisabled(true);
	scenarioDetailLayout->addWidget(scenarioMakeButton, 4, 2, 1, 1);
	connect(scenarioMessageSet, SIGNAL(activated(const QString &)), this, SLOT(loadMessageTypeList()));
	connect(scenarioMessageType, SIGNAL(activated(const QString &)), this, SLOT(loadMessageList()));
	connect(scenarioMakeButton, SIGNAL(clicked()), this, SLOT(makeScenario()));
	connect(addMessageButton, SIGNAL(clicked()), this, SLOT(addMessage()));
	connect(scenarioElementDeleteButton, SIGNAL(clicked()), this, SLOT(deleteMessage()));

	scenarioGroupBox->setLayout(scenarioDetailLayout);
	return scenarioGroupBox;
}


QGroupBox *sxsm::createScenarioViewerGroupBox()
{
	/*!
	* Places buttons, combo boxes, etc. in scenario viewer group box.
	*/
	scenarioMessageDetailLayout=new QGridLayout();
	QGroupBox *scenarioGroupBox = new QGroupBox(tr("ScenarioViewer"));
	scenarioSet = new QComboBox;
	scenarioSet->addItem(tr("Select scenario set"));
	scenarioSet->addItem(tr("Create new scenario set..."));
	scenarioNames = new QComboBox;
	scenarioNames->setDisabled(true);
	scenarioNames->addItem(tr("Select scenario"));
	if(!(loadScenarioSetList())){
		QMessageBox::StandardButton msgBox;
		msgBox = QMessageBox::critical(this, tr("Scenario set not loaded"),
                      tr("An error occured while loading scenario set list."),
                      QMessageBox::Ok);
	}
	scenarioDeleteButton = new QPushButton(tr("Delete scenario"), this);
	scenarioMessageDetailText = new QTextEdit();
	scenarioMessageDetailText->setAcceptRichText(false);
	scenarioSaveButton = new QPushButton(tr("Save scenario"), this);
	scenarioMessageDetailLayout->addWidget(scenarioSet, 0, 0, 1, 2);
	scenarioMessageDetailLayout->addWidget(scenarioNames, 0, 2, 1, 2);
	scenarioMessageDetailLayout->addWidget(scenarioDeleteButton, 2, 0);
	scenarioMessageDetailLayout->addWidget(scenarioMessageDetailText, 1, 0, 1, 4);
	scenarioMessageDetailLayout->addWidget(scenarioSaveButton, 2, 3);
	connect(scenarioSet, SIGNAL(activated(const QString &)), this, SLOT(loadScenarios()));
	connect(scenarioDeleteButton, SIGNAL(clicked()), this, SLOT(deleteScenario()));
	connect(scenarioNames, SIGNAL(activated(const QString &)), this, SLOT(viewScenario()));
  	connect(scenarioSaveButton, SIGNAL(clicked()), this, SLOT(makeNameDialog()));
	scenarioGroupBox->setLayout(scenarioMessageDetailLayout);
	return scenarioGroupBox;
}

bool sxsm::makeMessageSetCombo(){
	/*!
 	* Opens messageset.dat from scenario directory in read only mode.
 	*/
	QString fileName=MessageDir;
	fileName.append("messageset.dat");
	QFile file(fileName);
	if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
         return false;
	}else{
		/*!
 		* Clears message set combo box in case it was already filled.
 		*/
		int numberOfEntries = scenarioMessageSet->count();
		if(numberOfEntries>1){
			for(int i=1; i<numberOfEntries; i++){
				scenarioMessageSet->removeItem(1);
			}
		}
		/*!
 		* Adds entries of messageset.dat to the message set combo box
 		*/
		QTextStream in(&file);
		in.setCodec("UTF-8");
		while (!in.atEnd()) {
			QString line = in.readLine();
			scenarioMessageSet->addItem(line);
		}
		return true;
	}
}

bool sxsm::loadScenarioSetList()
{
	/*!
 	* Opens file scenariosets.dat from scenario directory in read only mode.
 	*/
	QString fileName=ScenarioDir;
	fileName.append("scenariosets.dat");
	QFile file(fileName);
	if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
         return false;
	}else{
		/*!
 		* Clears scenario set combo box in case it was already filled.
 		*/
		int numberOfEntries = scenarioSet->count();
		if(numberOfEntries>2){
			for(int i=2; i<numberOfEntries; i++){
				scenarioSet->removeItem(2);
			}
		}
		QTextStream in(&file);
		in.setCodec("UTF-8");
		/*!
 		* Adds entries of scenariosets.dat to the scenario set combo box
 		*/
		while (!in.atEnd()) {
			QString line = in.readLine();
			scenarioSet->addItem(line);
		}
		return true;
	}
}

bool sxsm::loadScenarioList()
{
	/*!
 	* Opens file scenarios.dat from currently selected scenario set directory
	* in read only mode.
 	*/
	QString fileName=ScenarioDir;
	fileName.append(scenarioSet->currentText());
	fileName.append("/scenarios.dat");
	QFile file(fileName);
	if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
         return false;
	}else{
		/*!
 		* Clears scenario list combo box, in case it was already filled.
 		*/
		int numberOfEntries = scenarioNames->count();
		if(numberOfEntries>1){
			for(int i=1; i<numberOfEntries; i++){
				scenarioNames->removeItem(1);
			}
		}
		/*!
 		* Adds entries of scenarios.dat to scenario list combo box.
 		*/
		QTextStream in(&file);
		in.setCodec("UTF-8");
		while (!in.atEnd()) {
			QString line = in.readLine();
			scenarioNames->addItem(line);
		}
		return true;
	}
}


bool sxsm::writeScenarioList()
{
	/*!
 	* Opens scenarios.dat file from currently selected scenario set in write only mode.
 	*/
	QString fileName=ScenarioDir;
	fileName.append(scenarioSet->currentText());
	fileName.append("/scenarios.dat");
	QFile file(fileName);
	if (!file.open(QIODevice::WriteOnly | QIODevice::Text)){
         return false;
	}else{
		/*!
 		* Writes entries of the scenario list combo box to scenarios.dat file.
 		*/
		QTextStream out(&file);
		out.setCodec("UTF-8");
		int numberOfEntries=scenarioNames->count();
		for(int i=1; i<numberOfEntries; i++){
				out << scenarioNames->itemText(i) << "\n";
		}
		return true;
	}
}

bool sxsm::writeScenarioDetail(QString scenarioName){
	/*!
 	* Creates xml file for a new scenario.
 	*/
	QString fileName=ScenarioDir;
	fileName.append(scenarioSet->currentText());
	fileName.append("/");
	QString tmpname=scenarioName;
	tmpname.append(".xml");
	fileName.append(tmpname);
	QFile file(fileName);
	/*!
	* Writes XML header and scenario content to xml file.
	*/
	if (!file.open(QIODevice::WriteOnly | QIODevice::Text)){
		return false;
	}else{
		QTextStream out(&file);
		out.setCodec("UTF-8");
		out << "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";
		out << "<!DOCTYPE scenario SYSTEM \"sipp.dtd\">\n\n";

		QString test = newMessageSetLine->text();
		QString tmpline = "<scenario name=\"";
		tmpline.append(test);
		tmpline.append("\">\n");
		out << tmpline;

		out << scenarioMessageDetailText->toPlainText();

		out << "</scenario>";
	}
	return true;
}

bool sxsm::loadScenarioDetail(QString scenarioName)
{
	/*!
	* Opens selected scenario file in read only mode.
	*/
	QString fileName=ScenarioDir;
	fileName.append(scenarioSet->currentText());
	fileName.append("/");
	QString tmpname=scenarioName;
	/*!
	* Filters out XML header.
	*/
	QRegExp rx("(^\\s*(<!))|(^<\\?)|(<scenario name)|(</scenario>)");
	tmpname.append(".xml");
	fileName.append(tmpname);
	QFile file(fileName);
	if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
	return false;
	}else{
		scenarioListWidget->clear();
		scenarioMessageDetailText->clear();
		QTextStream in(&file);
		in.setCodec("UTF-8");
		/*!
		* Displays content of scenario in scenario detail text edit.
		*/
		while (!in.atEnd()) {
			QString line = in.readLine();
			if(rx.indexIn(line)==-1){
				scenarioMessageDetailText->append(line);
			}
		}

	}
	return true;
}


void sxsm::loadScenarios(){
	/*!
	* Initiates loading of scenario list from selected scenario set.
	*/
	switch(scenarioSet->currentIndex()){
		case 0:
			break;
		case 1:
		/*!
		* If second entry from scenario set combo box is selected,
		* the dialog for creating a new set is displayed.
		*/
			makeCreateDialog();
			break;
		default:
		/*!
		* If loading of files via method loadScenarioList fails,
		* error message is generated.
		*/
			if(!(loadScenarioList())){
				QMessageBox::StandardButton msgBox;
				msgBox = QMessageBox::critical(this, tr("Scenarios not loaded"),
				tr("An error occured while loading scenario list."),
                      	QMessageBox::Ok);
			}else{
				scenarioNames->setDisabled(false);
			}

	}
}

void sxsm::makeCreateDialog(){
	/*!
	* Creates new modal dialog window.
	*/
	createDialog = new QDialog(this);
	createDialog->setWindowTitle("Create dialog");
	createDialog->resize(400,150);
	createDialog->setModal(true);
	createDialog->setSizeGripEnabled(false);
	QGridLayout *createLayout = new QGridLayout;
	newMessageSetLine = new QLineEdit(tr("Enter new set name"));
	newMessageSetButton = new QPushButton(tr("Create new set"));
	createLayout->addWidget(newMessageSetLine,0,0,1,2);
	createLayout->addWidget(newMessageSetButton,1,1);
	/*!
	* If newMessageSetButton is pushed, the slot makeSet() is called.
	*/
	connect(newMessageSetButton, SIGNAL(clicked()), this, SLOT(makeSet()));
	createDialog->setLayout(createLayout);
	createDialog->show();
}

void sxsm::makeNameDialog(){
	/*!
	* Creates new modal dialog window.
	*/
	scenarioNameDialog = new QDialog(this);
	scenarioNameDialog->setWindowTitle("Specify scenario Name");
	scenarioNameDialog->resize(400,150);
	scenarioNameDialog->setModal(true);
	scenarioNameDialog->setSizeGripEnabled(false);
	QGridLayout *createLayout = new QGridLayout;
	newMessageSetLine = new QLineEdit(tr("Enter scenario name"));
	newMessageSetButton = new QPushButton(tr("Save scenario"));
	createLayout->addWidget(newMessageSetLine,0,0,1,2);
	createLayout->addWidget(newMessageSetButton,1,1);
	/*!
	* If newMessageSetButton is pushed, the slot makeSet() is called.
	*/
	connect(newMessageSetButton, SIGNAL(clicked()), this, SLOT(saveScenario()));
	scenarioNameDialog->setLayout(createLayout);
	scenarioNameDialog->show();
}



void sxsm::makeSet()
{
	/*!
	* calls method createSet with new name as parameter.
	*/
	if(!(createSet(newMessageSetLine->text()))){
		QMessageBox::StandardButton msgBox;
		msgBox = QMessageBox::critical(this, tr("New set not created"),
                      tr("Couldn't create set in messages dir."),
                      QMessageBox::Ok);
	}
	/*!
	* Refreshes scenario set list after set was created.
	*/
	if(!(loadScenarioSetList())){
		QMessageBox::StandardButton msgBox;
		msgBox = QMessageBox::critical(this, tr("Couldn't update scenario set list"),
                      tr("Couldn't load scenario set list after modification."),
                      QMessageBox::Ok);
	}
	scenarioSet->setCurrentIndex(scenarioSet->count()-1);
	createDialog->close();
}

bool sxsm::createSet(QString dirname){
	/*!
	* Opens scenariosets.dat in read only mode.
	*/
	QString fileName=ScenarioDir;
	fileName.append("scenariosets.dat");
	QFile file(fileName);
	QStringList lines;
	if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
         return false;
	}else{
		/*!
		* Reads lines of scenariosets.dat and appends new set name.
		*/
		QTextStream in(&file);
		in.setCodec("UTF-8");
		while (!in.atEnd()) {
			lines << in.readLine();
		}
		lines << dirname;
		file.close();
	}
	/*!
	* Opens scenariosets.dat in write only mode and saves new entry permanently.
	*/
	if (!file.open(QIODevice::WriteOnly | QIODevice::Text)){
         return false;
	}else{
		QTextStream out(&file);
		out.setCodec("UTF-8");
		for(int i = 0; i < lines.size(); ++i){
			out << lines.at(i).toLocal8Bit().constData() << endl;
		}
		file.close();
	}
	/*!
	* Creates new scenario set directory and scenarios.dat within the new directory.
	*/
	QString setDirName=ScenarioDir;
	setDirName.append(dirname+"/");
	QDir myDir;
	if (!myDir.mkpath(setDirName)){
		return false;
	}
	setDirName.append("scenarios.dat");
	QFile file2(setDirName);
	if (!file2.open(QIODevice::WriteOnly | QIODevice::Text)){
		return false;
	}else{
		return true;
	}
}




bool sxsm::makeMessageTypeCombo(){
	/*!
	* Opens selected messagetype.dat in read only mode.
	*/
	QString fileName=MessageDir;
	fileName.append(scenarioMessageSet->currentText());
	fileName.append("/messagetype.dat");
	QFile file(fileName);
	if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
         return false;
	}else{
		/*!
		* Clears message type list, if it is already filled.
		*/
		int numberOfEntries = scenarioMessageType->count();
		if(numberOfEntries>1){
			for(int i=1; i<numberOfEntries; i++){
				scenarioMessageType->removeItem(1);
			}
		}
		QTextStream in(&file);
		in.setCodec("UTF-8");
		/*!
		* Adds every entry from selected messagetype.dat into message type combo box.
		*/
		while (!in.atEnd()) {
			QString line = in.readLine();
			scenarioMessageType->addItem(line);
		}
		return true;
	}

}

void sxsm::loadMessageList(){
	/*!
	* Initiates the execution of method makeMessageCombo.
	*/
	if(scenarioMessageType->currentIndex() > 0){
		if(!(makeMessageCombo())){
			QMessageBox::StandardButton msgBox;
			msgBox = QMessageBox::critical(this, tr("Messages not loaded"),
			tr("Couldn'find messages.dat in message dir."),
			QMessageBox::Ok);
		}else{
			scenarioMessage->setDisabled(false);
		}
	}
}

void sxsm::loadMessageTypeList(){
	/*!
	* Initiates the execution of method makeMessageTypeCombo.
	*/
	if(scenarioMessageSet->currentIndex() > 0){
		if(!(makeMessageTypeCombo())){
			QMessageBox::StandardButton msgBox;
			msgBox = QMessageBox::critical(this, tr("Messages type list not loaded"),
			tr("Couldn'find messagestype.dat in message dir."),
			QMessageBox::Ok);
		}else{
			scenarioMessageType->setDisabled(false);
		}
	}
}

bool sxsm::makeMessageCombo(){
	/*!
	* Opens messages.dat from selected message type directory.
	*/
	QString fileName=MessageDir;
	fileName.append(scenarioMessageSet->currentText());
	fileName.append("/");
	fileName.append(scenarioMessageType->currentText());
	fileName.append("/messages.dat");
	QFile file(fileName);
	if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
         return false;
	}else{
		/*!
		* Clears message combo box if it was already filled.
		*/
		int numberOfEntries = scenarioMessage->count();
		if(numberOfEntries>1){
			for(int i=1; i<numberOfEntries; i++){
				scenarioMessage->removeItem(1);
			}
		}
		/*!
		* Adds entries of messages.dat into message combo box.
		*/
		QTextStream in(&file);
		in.setCodec("UTF-8");
		while (!in.atEnd()) {
			QString line = in.readLine();
			scenarioMessage->addItem(line);
		}
		return true;
	}
}



void sxsm::deleteScenario()
{
	int numberOfEntries=scenarioNames->count();
	/*!
	* Checks if scenario list is not empty.
	*/
	if(numberOfEntries>1){
	QMessageBox msgBox;
	/*!
	* Locates file that should be deleted.
	*/
	QString deletefile = scenarioNames->currentText();
	QString fileName=ScenarioDir;
	fileName.append(scenarioSet->currentText());
	fileName.append("/");
	QString tmpname=deletefile;
	tmpname.append(".xml");
	fileName.append(tmpname);
	QFile file(fileName);
	/*!
	* Prompts if file really should be deleted.
	*/
	msgBox.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
	msgBox.setText(tr("Do you really want to delete current scenario?"));
		switch (msgBox.exec()) {
		case QMessageBox::Yes:
			/*!
			* Removes entry from scenario combo box.
			*/
			scenarioNames->removeItem(scenarioNames->currentIndex());
			numberOfEntries=scenarioNames->count();
			//scenarioNewName->setText(tr("Enter new name"));
			/*!
			* Removes xml file from HDD
			*/
			file.remove();
			writeScenarioList();
			scenarioMessageDetailText->clear();
			scenarioNames->setCurrentIndex(0);
			scenarioListWidget->clear();
			break;
		case QMessageBox::No:
			break;
		default:
			break;
		}
	} else {
		QMessageBox::StandardButton msgBox;
		msgBox = QMessageBox::warning(this, tr("No Scenarios left"),
                      tr("There are no scenarios left to be deleted."),
                      QMessageBox::Ok);
	}


}

void sxsm::saveScenario(){
	/*!
	* Adds new entry to scenario combo box.
	*/
	int numberOfEntries=scenarioNames->count();
	QString str = newMessageSetLine->displayText();
	scenarioNames->addItem(str);
	scenarioNames->setCurrentIndex((numberOfEntries));
	if(scenarioSet->currentIndex() > 1){
		/*!
		* initiates execution of method writeScenarioList(),
		* that writes down changes permanently to scenario list.
		*/
		if(writeScenarioList()){
			/*!
			* Initiates the execution of method writeScenarioDetail(),
			* that creates new scenario xml file.
			*/
			if(writeScenarioDetail(scenarioNames->currentText())){
				numberOfEntries=scenarioNames->count();
				//scenarioNewName->setText(tr("Scenario %1").arg(numberOfEntries));
				scenarioListWidget->clear();
				scenarioMessageDetailText->clear();
				scenarioNames->setCurrentIndex(0);
				QMessageBox::StandardButton msgBox;
				msgBox = QMessageBox::warning(this, tr("Scenarios Saved"),
				tr("All changes to the scenario list has been saved."),
				QMessageBox::Ok);
			}else{
				QMessageBox::StandardButton msgBox;
				msgBox = QMessageBox::critical(this, tr("File not saved"),
                      		tr("An error occured while writing file."),
                      		QMessageBox::Ok);
			}
		}
	}else{
		QMessageBox::StandardButton msgBox;
		msgBox = QMessageBox::critical(this, tr("Select a message set"),
		tr("Select a set where scenario should be saved."),
		QMessageBox::Ok);
	}
	scenarioNameDialog->close();
}
void sxsm::viewScenario(){
	QString tmpscenarioname = scenarioNames->currentText();
	if(scenarioNames->currentIndex() > 0){
		/*!
		* Initiates execution of method loadScenarioDetail(), that
		* loads the desired scenario into scenario viewer.
		*/
		if(!loadScenarioDetail(tmpscenarioname)){
			QMessageBox::StandardButton msgBox;
			msgBox = QMessageBox::critical(this, tr("Message details not loaded"),
			tr("An error occured while loading file."),
			QMessageBox::Ok);
		}
	}
}

void sxsm::addMessage(){
	/*!
	* Checks if a message has been selected.
	*/
	if((scenarioMessageType->currentIndex() != 0) && (scenarioMessageType->currentIndex() != 0) &&
	(scenarioMessage->currentIndex() != 0)) {
		//scenarioNewName->setDisabled(false);
		scenarioElementDeleteButton->setDisabled(false);
		scenarioListWidget->setDisabled(false);
		scenarioMakeButton->setDisabled(false);
		/*!
		* Appends the selected message to the message list widget.
		*/
		QString itemName=scenarioMessageSet->currentText();
		itemName.append("/");
		itemName.append(scenarioMessageType->currentText());
		itemName.append("/");
		itemName.append(scenarioMessage->currentText());
		new QListWidgetItem(itemName, scenarioListWidget);
	}
}

void sxsm::deleteMessage(){
	/*!
	* Steps through the message list widget and deletes every entry,
	* that was selected previously
	*/
	int numberOfListMessages=scenarioListWidget->count();
	int i=0;
	while(i<numberOfListMessages){
		if(scenarioListWidget->item(i)->isSelected()){
			scenarioListWidget->takeItem(i);
			if(numberOfListMessages==1){
				//scenarioNewName->setDisabled(true);
				scenarioElementDeleteButton->setDisabled(true);
				scenarioListWidget->setDisabled(true);
				scenarioMakeButton->setDisabled(true);
			}
		}else{
			i++;
		}
		numberOfListMessages=scenarioListWidget->count();
	}

}

void sxsm::makeScenario(){
	/*!
	* Creates scenario tag of a new scenario.
	*/
	scenarioMessageDetailText->clear();
	/*QString test = scenarioNewName->text();
	QString tmpline = "<scenario name=\"";
	tmpline.append(test);
	tmpline.append("\">");
	scenarioMessageDetailText->append(tmpline);*/
	/*!
	* Initiates execution of method makeMessage(), that creates the
	* xml message tags based on the entries of message list widget.
	*/
	if(!makeMessage()){
		QMessageBox::StandardButton msgBox;
		msgBox = QMessageBox::critical(this, tr("Could not load messages!"),
                      tr("An error occured while loading message files."),
                      QMessageBox::Ok);
	}
	//QString lastline ="</scenario>";
	//scenarioMessageDetailText->append(lastline);
}

bool sxsm::makeMessage(){
	/*!
	* Steps through the message message list widget.
	*/
	int numberOfListMessages=scenarioListWidget->count();
	for(int i=0; i<numberOfListMessages; i++){
		/*!
		* Opens each message file in read only mode.
		*/
		QString fileName=MessageDir;
		fileName.append(scenarioListWidget->item(i)->text());
		QFile file(fileName);
		if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
		return false;
		}else{
			/*!
			* Appends the loaded message to the scenario viewer.
			*/
			QTextStream in(&file);
			in.setCodec("UTF-8");
			while (!in.atEnd()) {
				QString line = in.readLine();
				scenarioMessageDetailText->append(line);
			}
		}
	}
	return true;
}
